﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace WebShopData
{
    public class User
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string PassWord { get; set; }
        public bool ActiveUser { get; set; }

        public User()
        {
                
        }

        public User(string userName, string passWord, bool activeUser, int userId=0)
        {
            UserName = userName;
            PassWord = passWord;
            ActiveUser = activeUser;
            UserID = userId;
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2} {3}", UserName, PassWord, ActiveUser, UserID );
        }
    }

    public static class Users
    {
        public static int ValidateLogin(string username, string password)
        {
            try
            {
                using (SqlConnection connection = DB.GetConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = @"SELECT * FROM Users WHERE Username = '{0}' AND Password = '{1}' AND ActiveUser = 1 ";
                        command.CommandText = string.Format(command.CommandText, username, password);

                        var dataset = new DataSet();
                        int affectedrows = new SqlDataAdapter(command).Fill(dataset);
                        return affectedrows;
                    }
                }
            }
            catch (Exception e)
            {
                DBException.AddException(e.Message, e.GetType().ToString());
                throw new Exception();
            }
        }

        public static int GetUserID(string username, string password)
        {
            try
            {
                using (SqlConnection connection = DB.GetConnection())
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = @"SELECT UserID FROM Users WHERE Username = '{0}' AND Password = '{1}'";
                        command.CommandText = string.Format(command.CommandText, username, password);

                        int userID = (int)command.ExecuteScalar();
                        return userID;
                    }
                }
            }
            catch (Exception e)
            {
                DBException.AddException(e.Message, e.GetType().ToString());
                throw new Exception();
            }
        }
    }
}


